<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2019/1/11
 * Time: 16:43
 */

namespace app\sports_admin\controller;
use app\exception\NewsException;
use app\sports_admin\model\News as newsModel;
use think\Request;
use app\sports_admin\vaildate\News as NewsService;
class News extends Base
{
    protected $beforeActionList = [
        'isResource'=>['only'=>'index,newlist,detail,updateheart,addpage,add,delheart'],
        'operationSelect'=>['only'=>'index,newlist,detail,addpage'],
        'operationInsert'=>['only'=>'add'],
        'operationUpdate'=>['only'=>'updateheart'],
        'operationDelete'=>['only'=>'delheart'],
    ];
    //体育资讯 页面
    public function index()
    {
        return view('index');
    }
    //体育资讯 列表
    public function newsList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','searchName']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $searchName = trim($params['searchName']);
        $result = newsModel::newsList($page,$rows,$sort,$sortOrder,$searchName);
        return json($result);
    }
    //体育资讯 详细信息页面
    public function detail()
    {
        $data = newsModel::detail();
        return view('detail',compact('data'));
    }
    //体育咨询 更新详细信息
    public function updateHeart(Request $request)
    {
        if($request->isAjax()){
            (new NewsService())->update();
            $data = (new newsModel())->updateHeart();
            if(!$data){
                throw new NewsException([
                    'msg'=>'修改失败',
                    'errorCode'=>'400'
                ]);
            }
            return json(['code'=>201,'msg'=>'修改成功']);
        }
    }
    //体育资讯 添加页面
    public function addPage()
    {
        return view("add");
    }
    //体育资讯 添加体育资讯
    public function add(Request $request)
    {
        if($request->isAjax()){
            (new NewsService())->insert();
            $data = newsModel::add();
            if(!$data){
                throw new NewsException([
                    'msg'=>'添加失败',
                    'errorCode'=>'400']);
            }
            return json(['code'=>201,'msg'=>'添加成功']);
        }
    }
    //删除体育资讯
    public function delHeart(Request $request)
    {
        if($request->isAjax()){
            (new NewsService())->go_check();
            $data = newsModel::delHeart();
            if(!$data){
                throw new NewsException([
                    'msg'=>'删除失败',
                    'errorCode'=>'400'
                ]);
            }
            return json(['code'=>201,'msg'=>'删除成功']);
        }
    }
}